﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ZquenceStudio3Host.DspPlugins.Synthesizers.Nodesizer.Engine.Modules.LowLevel
{
    [ModuleDisplay("Add 2")]
    class AddModule : BaseModule
    {
        public readonly ModuleInput InA;
        public readonly ModuleInput InB;
        public readonly ModuleOutput Out;

        public AddModule(ModuleManager manager)
            : base(manager)
        {
            // Add input and outputs to module.
            AddInput(InA = new ModuleInput("A", IOType.AudioOrWaveform, this));
            AddInput(InB = new ModuleInput("B", IOType.AudioOrWaveform, this));
            AddOutput(Out = new ModuleOutput("Out", IOType.AudioOrWaveform, this));
        }

        public override void Process()
        {
            // First check if both inputs sources is connected.
            if (InA.IsConnected && InB.IsConnected)
            {
                int length = Out.AudioBuffer.Length;
                double[] inA = InA.FromOutput.AudioBuffer.Buffer;
                double[] inB = InB.FromOutput.AudioBuffer.Buffer;
                double[] buffer = Out.AudioBuffer.Buffer;
                for (int i = 0; i < length; i++)
                {
                    buffer[i] = inA[i] + inB[i];
                }
            }
            else if (InA.IsConnected)
            {
                for (int i = 0; i < Out.AudioBuffer.Length; i++)
                {
                    Out.AudioBuffer[i] = InA.FromOutput.AudioBuffer[i];
                }
            }
            else if (InB.IsConnected)
            {
                for (int i = 0; i < Out.AudioBuffer.Length; i++)
                {
                    Out.AudioBuffer[i] = InB.FromOutput.AudioBuffer[i];
                }
            }
        }
    }
}
